package icu.twtool.generator

import icu.twtool.entity.Exercise
import icu.twtool.util.PathUtil
import org.junit.jupiter.api.Test

import org.junit.jupiter.api.Assertions.*
import java.io.PrintStream
import kotlin.io.path.outputStream

/**
 * 添加习题生成器单元测试
 * @author Wen
 * @since 2022-11-08
 */
class ExerciseGeneratorTest {

    private val equationCount = 50

    private val exerciseGenerator = ExerciseGenerator(equationCount)

    @Test
    fun generateOfAdditions() {
        val exercise = exerciseGenerator.generateOfAdditions()
        assertEquals(exercise.size, equationCount)
        PathUtil.home().resolve("temp").resolve("exercise-add.txt").let { path ->
            // 输出习题
            PrintStream(path.outputStream()).let {
                exercise.write(out = it)
                it.close()
                // 读取习题, 并检查数量
                assertEquals(Exercise.readFile(path).size, equationCount)
            }
        }

        PathUtil.home().resolve("temp").resolve("exercise-add-not-answer.txt").let { path ->
            // 输出习题
            PrintStream(path.outputStream()).let {
                exercise.write(answer = false, out = it)
                it.close()
                // 读取习题, 并检查数量
                assertEquals(Exercise.readFile(path).size, equationCount)
            }
        }
    }

    @Test
    fun generateOfSubtractions() {
        val exercise = exerciseGenerator.generateOfAdditions()
        assertEquals(exercise.size, equationCount)
        PathUtil.home().resolve("temp").resolve("exercise-sub.txt").let { path ->
            // 输出习题
            PrintStream(path.outputStream()).let {
                exercise.write(out = it)
                it.close()
                // 读取习题, 并检查数量
                assertEquals(Exercise.readFile(path).size, equationCount)
            }
        }

        PathUtil.home().resolve("temp").resolve("exercise-sub-not-answer.txt").let { path ->
            // 输出习题
            PrintStream(path.outputStream()).let {
                exercise.write(answer = false, out = it)
                it.close()
                // 读取习题, 并检查数量
                assertEquals(Exercise.readFile(path).size, equationCount)
            }
        }
    }

    @Test
    fun generateOfRandom() {
        val exercise = exerciseGenerator.generateOfAdditions()
        assertEquals(exercise.size, equationCount)
        PathUtil.home().resolve("temp").resolve("exercise-random.txt").let { path ->
            // 输出习题
            PrintStream(path.outputStream()).let {
                exercise.write(out = it)
                it.close()
                // 读取习题, 并检查数量
                assertEquals(Exercise.readFile(path).size, equationCount)
            }
        }

        PathUtil.home().resolve("temp").resolve("exercise-random-not-answer.txt").let { path ->
            // 输出习题
            PrintStream(path.outputStream()).let {
                exercise.write(answer = false, out = it)
                it.close()
                // 读取习题, 并检查数量
                assertEquals(Exercise.readFile(path).size, equationCount)
            }
        }
    }
}